package cuttings;

import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import java.util.Vector;
import java.awt.Rectangle;


import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;

import java.util.Date;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;

public class Player extends Sprite implements Cuttings_Commons{

    private final int START_Y = 420; 
    private final int START_X = 400;

    private final String player = "images/varun.png";
    private int width;
    private int height;
    private int score=0;
    
    static  private String player_name="";
    static private String player_team = "";
    
    static private FacebookClient facebookClient;
    static private String accessToken;

    public Player() {
		
		//String accessToken = "";
        //accessToken = "197457716932788|d3195084dba15165fddbc4a0-740198085|8sWtQwj2AhsNLa62ziiVGVw37Do";	

		ImageIcon ii = new ImageIcon(this.getClass().getResource(player));

		if(Menu.showImage == true)
			ii.setImage(Menu.profilePic());

		width = ii.getImage().getWidth(null); 
		height = ii.getImage().getHeight(null); 

		setImage(ii.getImage());
		setX(START_X);
		setY(START_Y);
		//publishFB("Hello world from cuttings");
    }
    
    static public void set_name(String n) {
		player_name =n;
	}
	
    static public void set_team(String n) {
		player_team = n;
	}	
	
	static public String get_name() {
		return player_name;
	}
	
	static public String get_team() {
		return player_team;
	}	
	
	public int getHeight() {
		return height;
	}
	
	public int getWidth() {
		return width;
	}	
	
	public void add_point(int p) {
		score+=p;
	}
	
	public int get_score() {
		return score;
	}
	
	static public void setToken(String s) {
		accessToken = s;
        facebookClient = new DefaultFacebookClient(accessToken);	
        System.out.println("\n\n$$$$$$$$FACEBOOK TOKEN is : " +s + "$$$$$$$$$$$$\n\n\n");		
	}

    public void act() {
        x += dx;
        if (x <= 180) 
            x = 180;
        if (x >= GAME_WIDTH - 2*width) 
            x = GAME_WIDTH - 2*width;     
    }
    public void keyPressed(KeyEvent e,Vector<Integer> x1,Vector<Integer> x2) {
        int key = e.getKeyCode();

        //timeDiff = System.currentTimeMillis() - beforeTime;

        //System.out.println("timeDiff is " + timeDiff);

        if (key == KeyEvent.VK_LEFT)
        {
            boolean gotHole = false;
                for(int i=0;i<x1.size();i++)
                {
                    //System.out.println("current coords is " +x + " x1[" + i +"] is " + x1.get(i) + " x2[" + i + "] is " + x2.get(i));				
                    if(x - x2.get(i) <=10 && x - x2.get(i) >=0)
                    {
                        gotHole = true;
                        break;
                    }
                }
            if(!gotHole) dx=-5;
            //else if(timeDiff<300) dx=0;   
            else dx=0;
        }

        if (key == KeyEvent.VK_RIGHT)
        {
            boolean gotHole = false;
                for(int i=0;i<x1.size();i++)
                {
                    if(x1.get(i) - x <=10 && x1.get(i) -x >=0)
                    {
                        gotHole = true;
                        break;
                    }
                }		
            if(!gotHole) dx=5;
            //else if(timeDiff<300) dx=0;            
            else dx=0;
        }
        //beforeTime = System.currentTimeMillis();

    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();


        if (key == KeyEvent.VK_LEFT)
        {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT)
        {
            dx = 0;
        }

    }
    
    static public void publishFB(String s) {
		System.out.println("\n\nToken is " + accessToken + " msg is " + s + "\n\n");
		facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", s));
    }    
    
    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }    
}
